package com.jmodata.zxu.controller.manager;

import com.jmodata.zxu.model.Tree;
import com.jmodata.zxu.pojo.manager.Manager;
import com.jmodata.zxu.pojo.manager.Operation;
import com.jmodata.zxu.pojo.manager.Role;
import com.jmodata.zxu.service.manager.ManagerService;
import com.jmodata.zxu.service.manager.MenuService;
import com.jmodata.zxu.service.manager.RoleService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("commonData")
public class CommonDataController {

	@Autowired(required = false)
	private RoleService roleService;

	@Autowired(required = false)
	private MenuService menuService;

	@Autowired(required = false)
	private ManagerService managerService;

	
	/**
	 * 获取所有角色
	 * 
	 * @return
	 */
	@RequestMapping("getRolesAll")
	@ResponseBody
	public List<Role> getRolesAll() {
		return roleService.getRolesList();
	}

	/**
	 * 获取用户操作权限
	 * 
	 * @param menuId
	 * @param request
	 * @return
	 */
	@RequestMapping("getGrant")
	@ResponseBody
	public List<Operation> getGrant(String menuId, HttpServletRequest request) {
		// 登陆用户信息
		Manager manager = (Manager) request.getSession().getAttribute("mgr");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menuId", menuId);
		map.put("managerId", manager.getManager_id());
		map.put("type", manager.getType());
		return menuService.getGrant(map);
	}

	/**
	 * 获取用户列表
	 * 
	 * @return
	 */
	@RequestMapping("getUsersAll")
	@ResponseBody
	public List<Manager> getUsersAll(HttpServletRequest request) {
		return managerService.getEmploysAllNoRole();
	}

	/**
	 * 获取一个社区所有管理员
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("getManagerByCommunityId")
	public void getManagerByCommunityId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		Manager manager = (Manager) request.getSession().getAttribute("mgr");
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("name", name);
		// 判断当前登陆用户是否是超级管理员以及是否填写社区id
		if (manager.getUserType().intValue() == 0) {
			List<Manager> list = managerService.getManagerByCommunityId(map);
			response.getWriter().print(JSONArray.fromObject(list));
		} else if (manager.getCommunityId().toString().equals("") || manager.getCommunityId().toString() == null) {
			throw new Exception("当前登陆用户没有社区归属！！");
		} else {
			map.put("communityId", manager.getCommunityId());
			List<Manager> list = managerService.getManagerByCommunityId(map);
			response.getWriter().print(JSONArray.fromObject(list));
		}
	}

	@RequestMapping("getTreeImg")
	@ResponseBody
	public List<Tree> getTreeImg() {
		List<Tree> trees = new ArrayList<Tree>();
		trees.add(new Tree("icon-note_edit", "icon-note_edit", "icon-note_edit"));
		trees.add(new Tree("icon-note_delete", "icon-note_delete", "icon-note_delete"));
		trees.add(new Tree("icon-key", "icon-key", "icon-key"));
		trees.add(new Tree("icon-zoom_out", "icon-zoom_out", "icon-zoom_out"));
		trees.add(new Tree("icon-zoom_in", "icon-zoom_in", "icon-zoom_in"));
		trees.add(new Tree("icon-test", "icon-test", "icon-test"));
		trees.add(new Tree("icon-user_edit", "icon-user_edit", "icon-user_edit"));
		trees.add(new Tree("icon-help", "icon-help", "icon-help"));
		trees.add(new Tree("icon-wrench", "icon-wrench", "icon-wrench"));
		trees.add(new Tree("icon-home", "icon-home", "icon-home"));
		trees.add(new Tree("icon-door_out", "icon-door_out", "icon-door_out"));
		trees.add(new Tree("icon-door_in", "icon-door_in", "icon-door_in"));
		trees.add(new Tree("icon-page", "icon-page", "icon-page"));
		trees.add(new Tree("icon-save", "icon-save", "icon-save"));
		trees.add(new Tree("icon-return", "icon-return", "icon-return"));
		trees.add(new Tree("icon-delete", "icon-delete", "icon-delete"));
		trees.add(new Tree("icon-arrow_refresh_small", "icon-arrow_refresh_small", "icon-arrow_refresh_small"));
		trees.add(new Tree("icon-arrow_refresh", "icon-arrow_refresh", "icon-arrow_refresh"));
		trees.add(new Tree("icon-ok", "icon-ok", "icon-ok"));
		trees.add(new Tree("icon-no", "icon-no", "icon-no"));
		trees.add(new Tree("icon-folder_table", "icon-folder_table", "icon-folder_table"));
		trees.add(new Tree("icon-folder_add", "icon-folder_add", "icon-folder_add"));
		trees.add(new Tree("icon-ruby_upload", "icon-ruby_upload", "icon-ruby_upload"));
		trees.add(new Tree("icon_new", "icon_new", "icon_new"));
		trees.add(new Tree("icon-world", "icon-world", "icon-world"));
		trees.add(new Tree("icon-add", "icon-add", "icon-add"));
		trees.add(new Tree("icon-edit", "icon-edit", "icon-edit"));
		return trees;
	}

	@RequestMapping("initRedis")
	@ResponseBody
	public void initRedis() {
		// RedisUtils redisUtils = RedisUtils.getInstance();
		// redisUtils.set("test", "hadoop");
	}

	@RequestMapping("getRedis")
	@ResponseBody
	public void getRedis() {
		// RedisUtils redisUtils = RedisUtils.getInstance();
		// System.out.println("````````````````````:" + redisUtils.getHashMapKeys("test"));
	}

}
